package com.hiwhitley.himei.module.video;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hiwhitley.himei.R;
import com.hiwhitley.himei.entity.IjkBean;
import com.hiwhitley.himei.entity.Video;
import com.hiwhitley.himei.entity.VideoDetail;
import com.hiwhitley.himei.widget.glide.GlideImageView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.drakeet.multitype.ItemViewProvider;

/**
 * Created by hiwhitley on 17-2-26.
 */
public class VideoTypeViewProvider
        extends ItemViewProvider<Video.IssueListBean.ItemListBean, VideoTypeViewProvider.ViewHolder> {


    @NonNull
    @Override
    protected ViewHolder onCreateViewHolder(
            @NonNull LayoutInflater inflater, @NonNull ViewGroup parent) {
        View root = inflater.inflate(R.layout.item_video_type, parent, false);
        return new ViewHolder(root);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, @NonNull Video.IssueListBean.ItemListBean videoType) {
        holder.mIvCover.loadNetImage(videoType.getData().getCover().getFeed(), R.drawable.bg_holder_brown);
        holder.mTvTime.setText(getTime(videoType.getData().getCategory(), videoType.getData().getDuration()));
        holder.mTvTitle.setText(videoType.getData().getTitle());

        holder.mLlMoban.setOnClickListener(v -> {
                    Video.IssueListBean.ItemListBean.DataBean data = videoType.getData();


                    List<IjkBean> list = new ArrayList<>();
                    for (Video.IssueListBean.ItemListBean.DataBean.PlayInfoBean playInfoBean : data.getPlayInfo()) {
                        IjkBean ijkBean = new IjkBean();
                        ijkBean.setStream(playInfoBean.getName());
                        ijkBean.setUrl(playInfoBean.getUrl());
                        list.add(ijkBean);
                    }

                    VideoDetail mVideoDetail = new VideoDetail();
                    mVideoDetail.setTitle(data.getTitle())
                            .setVideoijkBeanList(list)
                            .setBlurred(data.getCover().getBlurred())
                            .setCollect(data.getConsumption().getCollectionCount())
                            .setDesc(data.getDescription())
                            .setFeed(data.getCover().getFeed())
                            .setReply(data.getConsumption().getReplyCount())
                            .setShare(data.getConsumption().getShareCount())
                            .setVideo(data.getPlayUrl())
                            .setTime(getTime(data.getCategory(), data.getDuration()));

            Context context = holder.mLlMoban.getContext();
            Intent intent = VideoDetailActivity.newIntent(context, mVideoDetail);
                    ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                            (Activity) context, holder.mIvCover, VideoDetailActivity.TRANSIT_VIDEO);
                    try {
                        ActivityCompat.startActivity(context, intent, optionsCompat.toBundle());
                    } catch (IllegalArgumentException e) {
                        e.printStackTrace();
                        context.startActivity(intent);
                    }

                }
        );
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.iv_cover)
        GlideImageView mIvCover;
        @BindView(R.id.tv_title)
        TextView mTvTitle;
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.ll_moban)
        LinearLayout mLlMoban;

        ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }


    private String getTime(String category, long duration) {
        category = "#" + category + "  /  ";
        long last = duration % 60;
        String stringLast;
        if (last <= 9) {
            stringLast = "0" + last;
        } else {
            stringLast = last + "";
        }

        String durationString;
        long minit = duration / 60;
        if (minit < 10) {
            durationString = "0" + minit;

        } else {
            durationString = "" + minit;

        }
        return category + durationString + "' " + stringLast + '"';
    }
}